<html>
<head><meta charset="utf-8"><title>local-items in serde · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html">local-items in serde</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="195849215"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/local-items%20in%20serde/near/195849215" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html#195849215">(Apr 30 2020 at 14:44)</a>:</h4>
<p>I am checking why serde-derive do not works in RA, I just find it expanded to something like this:</p>
<div class="codehilite"><pre><span></span><code><span class="cp">#[allow(non_upper_case_globals, unused_attributes, unused_qualifications)]</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="n">_IMPL_SERIALIZE_FOR_Foo</span>: <span class="p">()</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[allow(unknown_lints)]</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[cfg_attr(feature = </span><span class="s">&quot;cargo-clippy&quot;</span><span class="cp">, allow(useless_attribute))]</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[allow(rust_2018_idioms)]</span><span class="w"></span>
<span class="w">    </span><span class="k">extern</span><span class="w"> </span><span class="k">crate</span><span class="w"> </span><span class="n">serde</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">_serde</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[allow(unused_macros)]</span><span class="w"></span>
<span class="w">    </span><span class="n">macro_rules</span><span class="o">!</span><span class="w"> </span><span class="k">try</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="cp">$__expr</span>:<span class="nc">expr</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span><span class="cp">$__expr</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">_serde</span>::<span class="n">export</span>::<span class="nb">Ok</span><span class="p">(</span><span class="n">__val</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">__val</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="n">_serde</span>::<span class="n">export</span>::<span class="nb">Err</span><span class="p">(</span><span class="n">__err</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="k">return</span><span class="w"> </span><span class="n">_serde</span>::<span class="n">export</span>::<span class="nb">Err</span><span class="p">(</span><span class="n">__err</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[automatically_derived]</span><span class="w"></span>
<span class="w">    </span><span class="k">impl</span><span class="w"> </span><span class="n">_serde</span>::<span class="n">Serialize</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">fn</span> <span class="nf">serialize</span><span class="o">&lt;</span><span class="n">__S</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">__serializer</span>: <span class="nc">__S</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">_serde</span>::<span class="n">export</span>::<span class="nb">Result</span><span class="o">&lt;</span><span class="n">__S</span>::<span class="nb">Ok</span><span class="p">,</span><span class="w"> </span><span class="n">__S</span>::<span class="n">Error</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">        </span><span class="k">where</span><span class="w"></span>
<span class="w">            </span><span class="n">__S</span>: <span class="nc">_serde</span>::<span class="n">Serializer</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="kd">let</span><span class="w"> </span><span class="n">__serde_state</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">try</span><span class="o">!</span><span class="p">(</span><span class="n">_serde</span>::<span class="n">Serializer</span>::<span class="n">serialize_struct</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="n">__serializer</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="s">&quot;Foo&quot;</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kc">false</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">usize</span><span class="w"></span>
<span class="w">            </span><span class="p">));</span><span class="w"></span>
<span class="w">            </span><span class="n">_serde</span>::<span class="n">ser</span>::<span class="n">SerializeStruct</span>::<span class="n">end</span><span class="p">(</span><span class="n">__serde_state</span><span class="p">)</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</code></pre></div>


<p>Note that the trait implementation is inside a block which is the RHS of const item. I don't think we handle this case in our collector phase, right ?</p>



<a name="195856209"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/local-items%20in%20serde/near/195856209" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html#195856209">(Apr 30 2020 at 15:27)</a>:</h4>
<p>nope. It's not really semi-local, it's just as local as items local to a function ;)</p>



<a name="195858047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/local-items%20in%20serde/near/195858047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html#195858047">(Apr 30 2020 at 15:40)</a>:</h4>
<p>oic, I changed the topic name ;)</p>



<a name="195858211"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/local-items%20in%20serde/near/195858211" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Edwin Cheng <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html#195858211">(Apr 30 2020 at 15:41)</a>:</h4>
<p>--delete--</p>



<a name="195859274"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/local-items%20in%20serde/near/195859274" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Florian Diebold <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/local-items.20in.20serde.html#195859274">(Apr 30 2020 at 15:49)</a>:</h4>
<p>and of course impls in blocks are pretty much the worst-case for local items...</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>